import os
import json
import logging

class Conf:
  __conf = {}
  if not os.path.exists(".conf"):
    with open(".conf","w",encoding="utf8") as f:
      f.write(json.dumps({
        
        "cookies": {
          "wt2":[""],
          "__zp_stoken__":""
          },
        "headers": {
          "User-Agent":""
          },
        "cities":"北京,上海,广州,深圳,杭州,天津,西安,苏州,武汉,厦门,长沙,成都,郑州,重庆,南京",
        "pageCount":4,
        "speed":{
          "mean":3,
          "sd":1,
        },
          "include":{
          "industries":"",
          "positionType":"",
          "position":""
        }
        }, ensure_ascii=False))
      pass
  else:
    with open(".conf","r",encoding="utf8") as f:
      __conf = json.loads(f.read())
  
  @staticmethod
  def getConf() -> dict:
    Conf.check()
    return Conf.__conf
  
  
  @staticmethod
  def check():
    if Conf.__conf == {}:
      raise RuntimeError("请配置.conf")
  
  @staticmethod
  def setStoken(stoken):
    Conf.check()
    Conf.__conf["cookies"]["__zp_stoken__"] = stoken
    
  @staticmethod
  def saveConfig():
    with open(".conf","w",encoding="utf-8") as f:
      
      f.write(json.dumps(Conf.__conf,ensure_ascii=False))
      logging.info("配置已保存")
    
    
  

  
    
  